__author__ = 'Kerr Wang'

import struct
import time
import os 
import time 
import random
import Encoder
import HttpPost
import DataAnalyze
import Teminal
import FileUtil
import serial
import sys
import logging
import log
import TimeGet
from config import *

def hex_to_string(hexNum):
	if not isinstance(hexNum, (int)):
		raise TypeError('bad operand type, parameter must be a int')
	char_tuple = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F')
	first_char = int(hexNum / 16)
	second_char = int(hexNum % 16)
	return char_tuple[first_char]+char_tuple[second_char]

def data_write(data):
	data_str = ''
	TimeStr = TimeGet.get_time_str();
	make_path = TimeStr.split(' ')[0]	
	for elem in data:
		data_str += hex_to_string(elem)	
		data_str += ' '	
	FileUtil.file_write(data_str,'raw_data',make_path)
	if DataAnalyze.data_correct(data, DATA_LENGTH):
		FileUtil.file_write(TimeStr+'\n','raw_data',make_path)
	else:
		FileUtil.file_write('error_data_'+TimeStr+'\n','raw_data',make_path)
